package erupt_simultaneously;


/**缓存行对其写法
 * @Auther Jun jie Yi
 * @Date 21:34 2021/7/30
 */
public class CacheLine {

    private static class T { //缓存行对齐
        public volatile long p1, p2, p3, p4, p5, p6, p7;
        public volatile long x = 0L;//让x在CPU缓存中独占缓存行

    }
    public static T[] arr = new T[2];
    static {
        arr[0] = new T();
        arr[1] = new T();
    }
    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 1000_0000L; i++) {
                arr[0].p1 = i;
            }
        });


        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 1000_0000L; i++) {
                arr[1].p1 = i;
            }
        });

        final long start = System.nanoTime();
        t1.start();
        t2.start();
        //将主线程
        t1.join();
        t2.join();
        System.out.println((System.nanoTime() - start) / 100_0000);
    }
}
